package org.M1B;


import java.sql.*;
import java.util.Vector;

public class Agente {
    protected static Agente mInstancia=null;
    protected static Connection mBD;
    //Constructor
    private Agente()throws Exception {}

    //Implementación del patrón Singleton
    public static Agente getAgente() throws Exception{
        if (mInstancia==null){
            mInstancia=new Agente();
        }
        return mInstancia;
    }

    private static void conectar() throws Exception {
        String driver="sun.jdbc.odbc.JdbcOdbcDriver";
        String url="jdbc:odbc:nombreODBC";
        Class.forName(driver);
        mBD=DriverManager.getConnection(url);
    }

    public static void desconectar() throws Exception{
        mBD.close();
    }

    public int insert(String SQL) throws SQLException, Exception{
        conectar();
		
        PreparedStatement ps = mBD.prepareStatement(SQL);
        int dev=ps.executeUpdate();
		
        ps.close();
        desconectar();
		
        return dev;
    }

    public Vector select(String SQL) throws SQLException, Exception{
        conectar();
		
        PreparedStatement ps = mBD.prepareStatement(SQL);
        ResultSet info = ps.executeQuery();
        ResultSetMetaData meta_info = info.getMetaData();
        Vector dats = new Vector(); // Vector a devolver
		
        while(info.next())
            dats.add(get_valor(info, meta_info));
			
        ps.close();
        desconectar();
		
        return dats;
    }
    // Método de apoyo, devuelve en un Vector un registro.
    private Vector get_valor(ResultSet info, ResultSetMetaData info_m)throws SQLException{
        Vector result = new Vector(); //Vector a devolver
        int n_columnas=info_m.getColumnCount();
		
        for(int i=1; i<=n_columnas; i++){
           switch(info_m.getColumnType(i)){
               case Types.INTEGER :
                   result.add(info.getInt(i));
                   break;
               case Types.DOUBLE :
                   result.add(info.getDouble(i));
                   break;
               case Types.VARCHAR :
                   result.add(info.getString(i));
                   break;
               case Types.BOOLEAN :
                   result.add(info.getBoolean(i));
                   break;
               case Types.DATE :
                   result.add(info.getDate(i));
                   break;
               case Types.NULL :
                   result.add(null);
                   break;
           }
        }
		
        return result;
    }

    public int delete(String SQL) throws SQLException, Exception{
        conectar();
		
        PreparedStatement ps = mBD.prepareCall(SQL);
        int var=ps.executeUpdate();
		
        ps.close();
        desconectar();
		
        return var;
    }

    public int update(String SQL) throws SQLException, Exception{
        conectar();
		
        PreparedStatement ps = mBD.prepareStatement(SQL);
        int var=ps.executeUpdate();
		
        ps.close();
        desconectar();
		
        return var;
    }
}
